﻿using ConAppCore.Json;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Numerics;
using System.Text;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.Log;
using UtilZ.Dotnet.Ex.Model;


namespace ConAppCore
{
    internal class TestConvertEx
    {
        public static void Test()
        {
            //TestCase();

            //TestCase2();

            //TestConvertToValue();

            //TestConvertExBasic();
            //TestConvertExBasic2();
            // TestValueType();

            //UInt16 value = 36225;

            //CPUTypes cpuType = CPUTypes.LittleEndian;
            //OutputTestBinaryEx(ConvertEx.ConvertToBitValues(value, cpuType), value, typeof(UInt16).Name);

            //OutputTestBinaryEx(ConvertToBitValues(value), value, typeof(UInt16).Name);

            //TestSignedConvert();
            //TestIntegerBitConvert();

            //TestHexadecimalStrToBuffer();
            //TestToHexadecimalString();

            //TestHexadecimalStringToBytes();


            //TestToBitValues();
            //TestToBitValues2(CPUTypes.LittleEndian);
            //Console.WriteLine("---------------------");
            //TestToBitValues2(CPUTypes.LargeEndian);

            TestToBitValues();

            //TestRotateMove();
            //TestRotateLeft();
            //TestRotateRight();

            //using static System.Runtime.InteropServices.JavaScript.JSType.String;
        }




        private static void TestHexadecimalStringToBytes()
        {
            string hexStr1 = System.IO.File.ReadAllText(@"D:\Temp\11.txt");
            using var poninter1 = new UnmanagedMemoryPointer<byte>(hexStr1.Length);
            poninter1.Length = ConvertEx.HexadecimaStringToBytes(hexStr1, poninter1.GetSpan());
            Console.WriteLine(poninter1.Length);

            string hexStr2 = System.IO.File.ReadAllText(@"D:\Temp\22.txt");
            using var poninter2 = new UnmanagedMemoryPointer<byte>(hexStr2.Length);
            poninter2.Length = ConvertEx.HexadecimaStringToBytes(hexStr2, poninter2.GetSpan());
            Console.WriteLine(poninter2.Length);

        }

        private static void TestValueType()
        {
            int targetResult;
            var ret = Int32.TryParse("1F", NumberStyles.HexNumber, null, out targetResult);

            var ret1 = ConvertEx.ConvertTo(typeof(Int32), "1F", NumberStyles.HexNumber);
            var ret2 = ConvertEx.ConvertTo(typeof(Int32), "1f", NumberStyles.HexNumber);

            ValueType sex = Sexs.Man;
            ValueType vt = System.Convert.ToInt16("123", 10);
            var value = (Int16)vt;
        }



        private static void TestToHexadecimalString()
        {
            ulong value = 123;
            string hexStr = value.ToString("x");

            try
            {
                //string hexStr1 = ConvertEx.ToHexadecimalString<sbyte>(-20);
                //string hexStr2 = ConvertEx.ToHexadecimalString<byte>(20);
                //string hexStr3 = ConvertEx.ToHexadecimalString<short>(-20);
                //string hexStr4 = ConvertEx.ToHexadecimalString<ushort>(20);
                //string hexStr5 = ConvertEx.ToHexadecimalString<int>(-20);
                //string hexStr6 = ConvertEx.ToHexadecimalString<uint>(20);
                //string hexStr7 = ConvertEx.ToHexadecimalString<long>(-20);
                //string hexStr8 = ConvertEx.ToHexadecimalString<ulong>(20);

                //string hexStr9 = ConvertEx.ToHexadecimalString<float>(-20.56f);
                //string hexStr10 = ConvertEx.ToHexadecimalString<double>(20.56d);
                //string hexStr11 = ConvertEx.ToHexadecimalString<decimal>(20.56m);

                string hexStr1 = ConvertEx.ToHexadecimaString(-20);
                string hexStr2 = ConvertEx.ToHexadecimaString(20);
                string hexStr3 = ConvertEx.ToHexadecimaString(-20);
                string hexStr4 = ConvertEx.ToHexadecimaString(20);
                string hexStr5 = ConvertEx.ToHexadecimaString(-20);
                string hexStr6 = ConvertEx.ToHexadecimaString(20);
                string hexStr7 = ConvertEx.ToHexadecimaString(-20);
                string hexStr8 = ConvertEx.ToHexadecimaString(20);

                string hexStr9 = ConvertEx.ToHexadecimaString((Half)20.56f);
                string hexStr10 = ConvertEx.ToHexadecimaString(-20.56f);
                string hexStr11 = ConvertEx.ToHexadecimaString(20.56d);
                //string hexStr12 = ConvertEx.ToHexadecimaString(20.56m);
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }

        }

        private static void TestIntegerBitConvert()
        {
            try
            {
                ByteSequence cpuType = ByteSequence.LittleEndian;

                sbyte v1 = -123;
                byte[] bits1 = ConvertEx.SbyteToBits(v1);
                sbyte v12 = ConvertEx.BitsToSbyte(bits1, 0);

                byte v2 = 123;
                byte[] bits2 = ConvertEx.ByteToBits(v2);
                byte v22 = ConvertEx.BitsToByte(bits2, 0);

                short v3 = -123;
                byte[] bits3 = ConvertEx.Int16ToBits(v3, cpuType);
                short v32 = ConvertEx.BitsToInt16(bits3, 0, cpuType);

                ushort v4 = 123;
                byte[] bits4 = ConvertEx.UInt16ToBits(v4, cpuType);
                ushort v42 = ConvertEx.BitsToUInt16(bits4, 0, cpuType);

                int v5 = -123;
                byte[] bits5 = ConvertEx.Int32ToBits(v5, cpuType);
                int v52 = ConvertEx.BitsToInt32(bits5, 0, cpuType);

                uint v6 = 123;
                byte[] bits6 = ConvertEx.UInt32ToBits(v6, cpuType);
                uint v62 = ConvertEx.BitsToUInt32(bits6, 0, cpuType);

                long v7 = -123;
                byte[] bits7 = ConvertEx.Int64ToBits(v7, cpuType);
                long v72 = ConvertEx.BitsToInt64(bits7, 0, cpuType);

                ulong v8 = 1230142;
                byte[] bits8 = ConvertEx.UInt64ToBits(v8, cpuType);
                ulong v82 = ConvertEx.BitsToUInt64(bits8, cpuType);
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestSignedConvert()
        {
            try
            {
                sbyte[] sbuffer1 = new sbyte[] { -10, 0, 10 };
                byte[] buffer1 = ConvertEx.ToUnsigned(sbuffer1);
                sbyte[] sbuffer2 = ConvertEx.ToSigned(buffer1);


                byte[] buffer3 = (byte[])ConvertEx.ToUnsigned<sbyte>(sbuffer1);
                sbyte[] sbuffer3 = (sbyte[])ConvertEx.ToSigned<byte>(buffer3);

                Int16[] int16Buffer1 = new Int16[] { -10, 0, 10 };
                UInt16[] uint16Buffer1 = (UInt16[])ConvertEx.ToUnsigned(int16Buffer1);
                Int16[] int16Buffer2 = (Int16[])ConvertEx.ToSigned(uint16Buffer1);

                Int32[] int32Buffer1 = new Int32[] { -10, 0, 10 };
                UInt32[] uint32Buffer1 = (UInt32[])ConvertEx.ToUnsigned(int32Buffer1);
                Int32[] int32Buffer2 = (Int32[])ConvertEx.ToSigned(uint32Buffer1);

            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private static void TestConvertExBasic2()
        {
            var ret1 = ConvertEx.ConvertTo(typeof(BigInteger), "213123".AsSpan());
            var ret2 = ConvertEx.ConvertTo(typeof(bool), "true".AsSpan());
            var ret3 = ConvertEx.ConvertTo(typeof(byte), "21".AsSpan());
            var ret4 = ConvertEx.ConvertTo(typeof(char), "a".AsSpan());
            var ret5 = ConvertEx.ConvertTo(typeof(DateOnly), "2020-12-23".AsSpan());
            var ret6 = ConvertEx.ConvertTo(typeof(DateTimeOffset), "2020-12-23 15:45:36".AsSpan());
            var ret7 = ConvertEx.ConvertTo(typeof(DateTime), "2020-12-23 15:45:36".AsSpan());
            var ret8 = ConvertEx.ConvertTo(typeof(Decimal), "213123".AsSpan());
            var ret9 = ConvertEx.ConvertTo(typeof(double), "213123".AsSpan());
            var ret10 = ConvertEx.ConvertTo(typeof(ConvertExEnum), ConvertExEnum.F.ToString().AsSpan());
            var ret11 = ConvertEx.ConvertTo(typeof(Int16), "-213".AsSpan());
            var ret12 = ConvertEx.ConvertTo(typeof(Int32), "-3123".AsSpan());
            var ret13 = ConvertEx.ConvertTo(typeof(Int32), "-213123".AsSpan());
            var ret14 = ConvertEx.ConvertTo(typeof(Int64), "-213123".AsSpan());

            var ret15 = ConvertEx.ConvertTo(typeof(sbyte), "-31".AsSpan());
            var ret16 = ConvertEx.ConvertTo(typeof(Single), "123.465".AsSpan());
            var ret17 = ConvertEx.ConvertTo(typeof(string), "safdsadf".AsSpan());
            var ret18 = ConvertEx.ConvertTo(typeof(TimeOnly), "15:45:36".AsSpan());
            var ret19 = ConvertEx.ConvertTo(typeof(UInt16), "2131".AsSpan());
            var ret20 = ConvertEx.ConvertTo(typeof(UInt32), "213123".AsSpan());
            var ret21 = ConvertEx.ConvertTo(typeof(UInt64), "213123".AsSpan());
        }

        private static void TestConvertExBasic()
        {
            var ret1 = ConvertEx.ConvertTo(typeof(BigInteger), "213123");
            var ret2 = ConvertEx.ConvertTo(typeof(bool), "true");
            var ret3 = ConvertEx.ConvertTo(typeof(byte), "21");
            var ret4 = ConvertEx.ConvertTo(typeof(char), "a");
            var ret5 = ConvertEx.ConvertTo(typeof(DateOnly), "2020-12-23");
            var ret6 = ConvertEx.ConvertTo(typeof(DateTimeOffset), "2020-12-23 15:45:36");
            var ret7 = ConvertEx.ConvertTo(typeof(DateTime), "2020-12-23 15:45:36");
            var ret8 = ConvertEx.ConvertTo(typeof(Decimal), "213123");
            var ret9 = ConvertEx.ConvertTo(typeof(double), "213123");
            var ret10 = ConvertEx.ConvertTo(typeof(ConvertExEnum), ConvertExEnum.F.ToString());
            var ret11 = ConvertEx.ConvertTo(typeof(Int16), "-213");
            var ret12 = ConvertEx.ConvertTo(typeof(Int32), "-3123");
            var ret13 = ConvertEx.ConvertTo(typeof(Int32), "-213123");
            var ret14 = ConvertEx.ConvertTo(typeof(Int64), "-213123");

            var ret15 = ConvertEx.ConvertTo(typeof(sbyte), "-31");
            var ret16 = ConvertEx.ConvertTo(typeof(Single), "123.465");
            var ret17 = ConvertEx.ConvertTo(typeof(string), "safdsadf");
            var ret18 = ConvertEx.ConvertTo(typeof(TimeOnly), "15:45:36");
            var ret19 = ConvertEx.ConvertTo(typeof(UInt16), "2131");
            var ret20 = ConvertEx.ConvertTo(typeof(UInt32), "213123");
            var ret21 = ConvertEx.ConvertTo(typeof(UInt64), "213123");
        }


        private static void TestRotateMove()
        {
            //左移
            /*************************************************
             * 01101100 -> 11011000  -> 10110001 -> 01100011
             * 
             * 
             *************************************************/

            //int mod = (value >> (8 - bits)) | 0;
            //int ret = (value << bits) | mod;
            //byte[] bitArr = ConvertEx.SbyteToBits((sbyte)ret);
            //Console.WriteLine(string.Join(string.Empty, bitArr));

            int bits;
            sbyte value = 0b01110001;

            //for (int i = 0; i < 20; i++)
            //{
            //    bits = i % 8;
            //    int mod = (value >> (8 - bits)) | 0;
            //    int ret = (value << bits) | mod;
            //    byte[] bitarr = ConvertEx.SbyteToBits((sbyte)ret);
            //    Console.WriteLine(string.Join(string.Empty, bitarr));
            //}


            //右移
            /*************************************************
           * 01101100 -> 00110110  -> 00011011 -> 10001101
           * 10001101
           * 
           *************************************************/
            for (int i = 0; i < 20; i++)
            {
                bits = i % 8;
                int mod = (value << (8 - bits)) | 0;
                int ret = (value >> bits) | mod;
                byte[] bitarr = ConvertEx.SbyteToBits((sbyte)ret);
                Console.WriteLine(string.Join(string.Empty, bitarr));
            }
        }

        private static void TestRotateLeft()
        {
            ByteSequence cpu = ByteSequence.LittleEndian;
            //sbyte value = 0b01110001;
            //for (int i = 0; i < 20; i++)
            //{
            //    byte[] bitArr = ConvertEx.SbyteToBits(ConvertEx.RotateLeft(value, i));
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //byte value = 0b01110001;
            //for (int i = 0; i < 20; i++)
            //{
            //    byte[] bitArr = ConvertEx.ByteToBits(ConvertEx.RotateLeft(value, i));
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}


            //short value = 0b01110001;
            //for (int i = 0; i < 20; i++)
            //{
            //    byte[] bitArr = ConvertEx.Int16ToBits(ConvertEx.RotateLeft(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //ushort value = 0b01110001;
            //for (int i = 0; i < 20; i++)
            //{
            //    byte[] bitArr = ConvertEx.UInt16ToBits(ConvertEx.RotateLeft(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //int value = 0b01110001;
            //for (int i = 0; i < 40; i++)
            //{
            //    byte[] bitArr = ConvertEx.Int32ToBits(ConvertEx.RotateLeft(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //uint value = 0b01110001;
            //for (int i = 0; i < 40; i++)
            //{
            //    byte[] bitArr = ConvertEx.UInt32ToBits(ConvertEx.RotateLeft(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //long value = 0b01110001;
            //for (int i = 0; i < 80; i++)
            //{
            //    byte[] bitArr = ConvertEx.Int64ToBits(ConvertEx.RotateLeft(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //ulong value = 0b01110001;
            //for (int i = 0; i < 80; i++)
            //{
            //    byte[] bitArr = ConvertEx.UInt64ToBits(ConvertEx.RotateLeft(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //Int128 value = 0b01110001;
            //for (int i = 0; i < 140; i++)
            //{
            //    byte[] bitArr = ConvertEx.Int128ToBits(ConvertEx.RotateLeft(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            UInt128 value = 0b01110001;
            for (int i = 0; i < 140; i++)
            {
                byte[] bitArr = ConvertEx.UInt128ToBits(ConvertEx.RotateLeft(value, i), cpu);
                Console.WriteLine(string.Join(string.Empty, bitArr));
            }
        }

        private static void TestRotateRight()
        {
            ByteSequence cpu = ByteSequence.LittleEndian;

            //sbyte value = 0b01110001;
            //for (int i = 0; i < 20; i++)
            //{
            //    byte[] bitArr = ConvertEx.SbyteToBits(ConvertEx.RotateRight(value, i));
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //byte value = 0b01110001;
            //for (int i = 0; i < 20; i++)
            //{
            //    byte[] bitArr = ConvertEx.ByteToBits(ConvertEx.RotateRight(value, i));
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}


            //short value = 0b01110001;
            //for (int i = 0; i < 20; i++)
            //{
            //    byte[] bitArr = ConvertEx.Int16ToBits(ConvertEx.RotateRight(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //ushort value = 0b01110001;
            //for (int i = 0; i < 20; i++)
            //{
            //    byte[] bitArr = ConvertEx.UInt16ToBits(ConvertEx.RotateRight(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //int value = 0b01110001;
            //for (int i = 0; i < 40; i++)
            //{
            //    byte[] bitArr = ConvertEx.Int32ToBits(ConvertEx.RotateRight(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //uint value = 0b01110001;
            //for (int i = 0; i < 40; i++)
            //{
            //    byte[] bitArr = ConvertEx.UInt32ToBits(ConvertEx.RotateRight(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //long value = 0b01110001;
            //for (int i = 0; i < 80; i++)
            //{
            //    byte[] bitArr = ConvertEx.Int64ToBits(ConvertEx.RotateRight(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //ulong value = 0b01110001;
            //for (int i = 0; i < 80; i++)
            //{
            //    byte[] bitArr = ConvertEx.UInt64ToBits(ConvertEx.RotateRight(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            //Int128 value = 0b01110001;
            //for (int i = 0; i < 140; i++)
            //{
            //    byte[] bitArr = ConvertEx.Int128ToBits(ConvertEx.RotateRight(value, i), cpu);
            //    Console.WriteLine(string.Join(string.Empty, bitArr));
            //}

            UInt128 value = 0b01110001;
            for (int i = 0; i < 140; i++)
            {
                byte[] bitArr = ConvertEx.UInt128ToBits(ConvertEx.RotateRight(value, i), cpu);
                Console.WriteLine(string.Join(string.Empty, bitArr));
            }
        }




        private static void TestToBitValues()
        {
            sbyte orginValue1 = 0b01100101;//101
            byte[] bits1 = ConvertEx.SbyteToBits(orginValue1);
            sbyte retValue1 = ConvertEx.BitsToSbyte(bits1);
            string retStr1 = string.Join(string.Empty, bits1);


            //----------------------------------------
            byte orginValue2 = 0b01100101;//101
            byte[] bits2 = ConvertEx.ByteToBits(orginValue2);
            sbyte retValue2 = ConvertEx.BitsToSbyte(bits2);
            string retStr2 = string.Join(string.Empty, bits2);

            //----------------------------------------
            short orginValue3 = 0b00000000000000000000000101101101;//365
            byte[] bitsLittle3 = ConvertEx.Int16ToBits(orginValue3, ByteSequence.LittleEndian);
            byte[] bitsLarge3 = ConvertEx.Int16ToBits(orginValue3, ByteSequence.LargeEndian);
            long retValueLittle = ConvertEx.BitsToInt16(bitsLittle3, ByteSequence.LittleEndian);
            long retValueLarge = ConvertEx.BitsToInt16(bitsLarge3, ByteSequence.LargeEndian);
            string littleStr3 = string.Join(string.Empty, bitsLittle3);
            string largeoStr3 = string.Join(string.Empty, bitsLarge3);

            //----------------------------------------
            ushort orginValue4 = 0b00000000000000000000000101101101;//365
            byte[] bitsLittle4 = ConvertEx.UInt16ToBits(orginValue4, ByteSequence.LittleEndian);
            byte[] bitsLarge4 = ConvertEx.UInt16ToBits(orginValue4, ByteSequence.LargeEndian);
            long retValueLittle4 = ConvertEx.BitsToUInt16(bitsLittle4, ByteSequence.LittleEndian);
            long retValueLarge4 = ConvertEx.BitsToUInt16(bitsLarge4, ByteSequence.LargeEndian);
            string littleStr4 = string.Join(string.Empty, bitsLittle4);
            string largeoStr4 = string.Join(string.Empty, bitsLarge4);

            //----------------------------------------
            int orginValue5 = 0b00000000000000000000000101101101;//365
            byte[] bitsLittle5 = ConvertEx.Int32ToBits(orginValue5, ByteSequence.LittleEndian);
            byte[] bitsLarge5 = ConvertEx.Int32ToBits(orginValue5, ByteSequence.LargeEndian);
            long retValueLittle5 = ConvertEx.BitsToInt32(bitsLittle5, ByteSequence.LittleEndian);
            long retValueLarge5 = ConvertEx.BitsToInt32(bitsLarge5, ByteSequence.LargeEndian);
            string littleStr5 = string.Join(string.Empty, bitsLittle5);
            string largeoStr5 = string.Join(string.Empty, bitsLarge5);

            //小端
            byte[] int32bytes = BitConverter.GetBytes(orginValue5);
            StringBuilder sb = new StringBuilder(32);
            for (int i = 0; i < int32bytes.Length; i++)
            {
                sb.Append(Convert.ToString(int32bytes[i], 2).PadLeft(8, '0'));
            }
            string int32bitStr = sb.ToString();



            //----------------------------------------
            uint orginValue6 = 0b101101101;//365
            byte[] bitsLittle6 = ConvertEx.UInt32ToBits(orginValue6, ByteSequence.LittleEndian);
            byte[] bitsLarge6 = ConvertEx.UInt32ToBits(orginValue6, ByteSequence.LargeEndian);
            long retValueLittle6 = ConvertEx.BitsToUInt32(bitsLittle6, ByteSequence.LittleEndian);
            long retValueLarge6 = ConvertEx.BitsToUInt32(bitsLarge6, ByteSequence.LargeEndian);
            string littleStr6 = string.Join(string.Empty, bitsLittle6);
            string largeoStr6 = string.Join(string.Empty, bitsLarge6);

            //----------------------------------------
            long orginValue7 = 0b101101101101101101;//187245
            byte[] bitsLittle7 = ConvertEx.Int64ToBits(orginValue7, ByteSequence.LittleEndian);
            byte[] bitsLarge7 = ConvertEx.Int64ToBits(orginValue7, ByteSequence.LargeEndian);
            long retValueLittle7 = ConvertEx.BitsToInt64(bitsLittle7, ByteSequence.LittleEndian);
            long retValueLarge7 = ConvertEx.BitsToInt64(bitsLarge7, ByteSequence.LargeEndian);
            string littleStr7 = string.Join(string.Empty, bitsLittle7);
            string largeoStr7 = string.Join(string.Empty, bitsLarge7);

            //----------------------------------------
            ulong orginValue8 = 0b101101101101101101;//187245
            byte[] bitsLittle8 = ConvertEx.UInt64ToBits(orginValue8, ByteSequence.LittleEndian);
            byte[] bitsLarge8 = ConvertEx.UInt64ToBits(orginValue8, ByteSequence.LargeEndian);
            ulong retValueLittle8 = ConvertEx.BitsToUInt64(bitsLittle8, ByteSequence.LittleEndian);
            ulong retValueLarge8 = ConvertEx.BitsToUInt64(bitsLarge8, ByteSequence.LargeEndian);
            string littleStr8 = string.Join(string.Empty, bitsLittle8);
            string largeoStr8 = string.Join(string.Empty, bitsLarge8);

            //----------------------------------------
            Int128 orginValue9 = 123456789;
            byte[] bitsLittle9 = ConvertEx.Int128ToBits(orginValue9, ByteSequence.LittleEndian);
            byte[] bitsLarge9 = ConvertEx.Int128ToBits(orginValue9, ByteSequence.LargeEndian);
            Int128 retValueLittle9 = ConvertEx.BitsToInt128(bitsLittle9, ByteSequence.LittleEndian);
            Int128 retValueLarge9 = ConvertEx.BitsToInt128(bitsLarge9, ByteSequence.LargeEndian);
            string littleStr9 = string.Join(string.Empty, bitsLittle9);
            string largeoStr9 = string.Join(string.Empty, bitsLarge9);

            //----------------------------------------
            UInt128 orginValue10 = 123456789;
            byte[] bitsLittle10 = ConvertEx.UInt128ToBits(orginValue10, ByteSequence.LittleEndian);
            byte[] bitsLarge10 = ConvertEx.UInt128ToBits(orginValue10, ByteSequence.LargeEndian);
            UInt128 retValueLittle10 = ConvertEx.BitsToUInt128(bitsLittle10, ByteSequence.LittleEndian);
            UInt128 retValueLarge10 = ConvertEx.BitsToUInt128(bitsLarge10, ByteSequence.LargeEndian);
            string littleStr10 = string.Join(string.Empty, bitsLittle10);
            string largeoStr10 = string.Join(string.Empty, bitsLarge10);

            //----------------------------------------
            //BigInteger orginValue = 123456789;
            //byte[] bitsLittle1 = ConvertEx.BigIntegerToBits(orginValue, CPUTypes.LittleEndian);
            //byte[] bitsLarge1 = ConvertEx.BigIntegerToBits(orginValue, CPUTypes.LargeEndian);
            //BigInteger retValueLittle = ConvertEx.BitsToBigInteger(bitsLittle1, CPUTypes.LittleEndian);
            //BigInteger retValueLarge = ConvertEx.BitsToBigInteger(bitsLarge1, CPUTypes.LargeEndian);
            //string littleStr11 = string.Join(string.Empty ,bitsLittle1);
            //string largeoStr11 = string.Join(string.Empty, bitsLarge1);


            //----------------------------------------
            float orginValue12 = 123456.789f;
            byte[] bitsLittle12 = ConvertEx.FloatToBits(orginValue12, ByteSequence.LittleEndian);
            byte[] bitsLarge12 = ConvertEx.FloatToBits(orginValue12, ByteSequence.LargeEndian);
            float retValueLittle12 = ConvertEx.BitsToFloat(bitsLittle12, ByteSequence.LittleEndian);
            float retValueLarge12 = ConvertEx.BitsToFloat(bitsLarge12, ByteSequence.LargeEndian);
            string littleStr12 = string.Join(string.Empty, bitsLittle12);
            string largeoStr12 = string.Join(string.Empty, bitsLarge12);

            //----------------------------------------
            double orginValue13 = 123456.789d;
            byte[] bitsLittle13 = ConvertEx.DoubleToBits(orginValue13, ByteSequence.LittleEndian);
            byte[] bitsLarge13 = ConvertEx.DoubleToBits(orginValue13, ByteSequence.LargeEndian);
            double retValueLittle13 = ConvertEx.BitsToDouble(bitsLittle13, ByteSequence.LittleEndian);
            double retValueLarge13 = ConvertEx.BitsToDouble(bitsLarge13, ByteSequence.LargeEndian);
            string littleStr13 = string.Join(string.Empty, bitsLittle13);
            string largeoStr13 = string.Join(string.Empty, bitsLarge13);

            //----------------------------------------
            decimal orginValue14 = 123456.789m;
            byte[] bitsLittle14 = ConvertEx.DecimalToBits(orginValue14, ByteSequence.LittleEndian);
            byte[] bitsLarge14 = ConvertEx.DecimalToBits(orginValue14, ByteSequence.LargeEndian);
            decimal retValueLittle14 = ConvertEx.BitsToDecimal(bitsLittle14, 0, ByteSequence.LittleEndian);
            decimal retValueLarge14 = ConvertEx.BitsToDecimal(bitsLarge14, 0, ByteSequence.LargeEndian);
            string littleStr14 = string.Join(string.Empty, bitsLittle14);
            string largeoStr14 = string.Join(string.Empty, bitsLarge14);
        }






        private static byte[] ConvertToBitValues(UInt16 value)
        {
            const int SIZE = 16;
            string byteStr = Convert.ToString(value, 2);
            if (byteStr.Length < SIZE)
            {
                byteStr = byteStr.Insert(0, new string('0', SIZE - byteStr.Length));
            }

            byte[] bitValueArr = new byte[SIZE];
            //for (int i = 0; i < SIZE; i++)
            //{
            //    bitValueArr[i] = byte.Parse(byteStr[i].ToString());
            //}

            int index = 0;
            for (int i = SIZE - 1; i >= 0; i--)
            {
                bitValueArr[index] = byte.Parse(byteStr[i].ToString());
                index++;
            }

            return bitValueArr;
        }




        public enum ConvertExEnum
        {
            F,

            M
        }
    }


}
